package com.ruoyi.system.service.impl;

import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.system.api.domain.ShProductPicture;
import com.ruoyi.system.utils.TencentUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ShProductPictureMapper;
import com.ruoyi.system.service.IShProductPictureService;

/**
 * 商品图片Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-07-18
 */
@Service
public class ShProductPictureServiceImpl implements IShProductPictureService 
{
    @Autowired
    private ShProductPictureMapper shProductPictureMapper;

    /**
     * 查询商品图片
     * 
     * @param id 商品图片主键
     * @return 商品图片
     */
    @Override
    public ShProductPicture selectShProductPictureById(Long id)
    {
        return shProductPictureMapper.selectShProductPictureById(id);
    }

    /**
     * 查询商品图片列表
     * 
     * @param shProductPicture 商品图片
     * @return 商品图片
     */
    @Override
    public List<ShProductPicture> selectShProductPictureList(ShProductPicture shProductPicture)
    {
        return shProductPictureMapper.selectShProductPictureList(shProductPicture);
    }

    /**
     * 新增商品图片
     * 
     * @param shProductPicture 商品图片
     * @return 结果
     */
    @Override
    public int insertShProductPicture(ShProductPicture shProductPicture)
    {
        shProductPicture.setCreateTime(DateUtils.getNowDate());
        return shProductPictureMapper.insertShProductPicture(shProductPicture);
    }

    /**
     * 修改商品图片
     * 
     * @param shProductPicture 商品图片
     * @return 结果
     */
    @Override
    public int updateShProductPicture(ShProductPicture shProductPicture)
    {
        return shProductPictureMapper.updateShProductPicture(shProductPicture);
    }

    /**
     * 批量删除商品图片
     * 
     * @param ids 需要删除的商品图片主键
     * @return 结果
     */
    @Override
    public int deleteShProductPictureByIds(Long[] ids)
    {
        TencentUtils tencentUtils = TencentUtils.getInstance();
        for (Long id : ids) {
            ShProductPicture item = shProductPictureMapper.selectShProductPictureById(id);
            if (null != item.getProductPic() && item.getProductPic().indexOf("aliyuncs.com/") > 0) {
                String[] keyAry = item.getProductPic().split("aliyuncs.com/");
                tencentUtils.del(keyAry[1]);
            }
            shProductPictureMapper.deleteShProductPictureById(id);
        }
        return ids.length;
    }

    /**
     * 删除商品图片信息
     * 
     * @param id 商品图片主键
     * @return 结果
     */
    @Override
    public int deleteShProductPictureById(Long id)
    {
        TencentUtils tencentUtils = TencentUtils.getInstance();
        ShProductPicture item = shProductPictureMapper.selectShProductPictureById(id);
        if (null != item.getProductPic() && item.getProductPic().indexOf("aliyuncs.com/") > 0) {
            String[] keyAry = item.getProductPic().split("aliyuncs.com/");
            tencentUtils.del(keyAry[1]);
        }
        return shProductPictureMapper.deleteShProductPictureById(id);
    }
}
